<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="80px">
      <el-form-item label="设备号" prop="equNumber">
        <el-input v-model="dataForm.equNumber" placeholder="设备号"></el-input>
      </el-form-item>
      <!-- <el-form-item label="设备名" prop="equName">
        <el-input v-model="dataForm.equName" placeholder="设备名"></el-input>
      </el-form-item> -->
      <!-- <el-form-item  label="用户Id" prop="userids">
        <el-input  v-model="dataForm.userids" placeholder="用户Id"></el-input>
      </el-form-item> -->
      <el-form-item label="用户名" prop="usernames">
        <el-select style="width: 100%;" multiple v-model="dataForm.usernames" @change="chengeSelect" placeholder="请选择">
          <el-option v-for="item in nameList" :key="item.value" :label="item.label" :value="item.label">
          </el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="设备状态" prop="equStauts">

        <el-switch @change=changeEquStauts v-model="dataForm.equStauts" :active-text="swichName" active-value='1'
          inactive-value='0' active-color="#13ce66" inactive-color="#b2b2b2">
        </el-switch>



      </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data() {

      return {
        namex:'',
        swichName: '正常',
        visible: false,
        nameList: [],
        dataForm: {
          id: 0,
          equNumber: '',
          equName: '',
          userids: [],
          usernames: [],
          equStauts: '1'
        },
        dataRule: {
          equNumber: [{
            required: true,
            message: '设备号不能为空',
            trigger: 'blur'
          }],
          equName: [{
            required: true,
            message: '设备名不能为空',
            trigger: 'blur'
          }],
          userids: [{
            required: true,
            message: '用户Id不能为空',
            trigger: 'blur'
          }],
          usernames: [{
            required: true,
            message: '用户名不能为空',
            trigger: 'change'
          }],
          equStauts: [{
            required: true,
            message: '设备状态 0禁用，1开启不能为空',
            trigger: 'change'
          }]
        }
      }
    },
    created() {
      // this.getUserInfo()
      this.getDataList()
    },
    methods: {
      chengeSelect(e) {
        console.log(e);
         let obj = [];
         this.dataForm.userids=[]
        for(var i=0;i<e.length;i++){

          obj[i] = this.nameList.find((item) => { //这里的userList就是上面遍历的数据源
            return item.label === e[i]; //筛选出匹配数据
          });
           this.dataForm.userids[i]=obj[i].value
        }
        console.log(obj)
        // for(var i=0;i<obj.length;i++){

        //   this.dataForm.userids[i]=obj[i].label
        // }
        console.log(this.dataForm.userids, this.dataForm.usernames)
      },
      getDataList() {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/sys/user/list'),
          method: 'get',
        }).then(({
          data
        }) => {
          console.log('用户', data)
          for (var i = 0; i < data.page.list.length; i++) {
           if(data.page.list[i].username!='admin'){
			   this.nameList.push({
				   value:data.page.list[i].userId,
				   label:data.page.list[i].username
			   })
		   }
          }
          console.log(this.nameList)
          this.$forceUpdate()
        })
      },
      changeEquStauts() {
        console.log(this.dataForm.equStauts)
        if (this.dataForm.equStauts == 1) {
          this.swichName = '正常'
        } else {
          this.swichName = '异常'
        }
      },
      init(id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/equ/manage/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({
              data
            }) => {
               console.log(data)
              if (data && data.code === 0) {
                this.dataForm.equNumber = data.dyEqu.equNumber
                this.dataForm.equName = data.dyEqu.equName
                this.dataForm.userids = data.dyEqu.userIds.split(',')
                this.dataForm.usernames = data.dyEqu.userNames.split(',')
                // if(data.dyEqu.equStatus=='正常'){
                //   this.dataForm.equStauts = '1'
                // }else{
                //   this.dataForm.equStauts = '0'
                // }


              }
              console.log('adsad=',this.dataForm)

            })
          }
        })
      },
      // 表单提交
      getUserInfo() {


        this.$http({
          url: this.$http.adornUrl(`/sys/user/info`),
          method: 'get',
        }).then((data) => {

          console.log(data.data.user.userId)
          this.dataForm.userids = data.data.user.userId
          this.dataForm.usernames = data.data.user.username
        })


      },
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
           // console.log(this.dataForm.equStauts)
            // if(this.dataForm.equStauts==1){
            //   var astatus='正常'
            // }else{
            //   var astatus='异常'
            // }
           // return
			  var userids=[]
			  var usernames=[]
			  for(var i=0; i<this.dataForm.userids.length;i++){
            userids.push(this.dataForm.userids[i])
            usernames.push(this.dataForm.usernames[i])
			  }
            userids = userids.toString();  //把数组转换为字符串
            usernames= usernames.toString();  //把数组转换为字符串
            console.log(this.dataForm)
            this.$http({
              url: this.$http.adornUrl(`/equ/manage/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'equNumber': this.dataForm.equNumber,
                'equName': this.dataForm.equName,
                'userIds': userids,
                'userNames': usernames,
              })
            }).then(({
              data
            }) => {
              console.log(data)
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
